<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class profile extends CI_Controller{
    private $imageRoot="datastore";
    public function __constuctor(){
        parent::__construct();

    }
   public function index(){
        $sesData=$this->session->userdata('userData');
        $this->load->model("event_md","eventCls");
        if($sesData!=false){
            $userId=$sesData['userId'];
            $query ="SELECT u.fName,u.lName,u.userId,u.email,ui.userInterest,ui.userBio,ui.userCity,ui.userState,
            u.datastorePath, u.hasIcon FROM user AS u LEFT JOIN userinfo AS ui ON ui.userId=u.userId WHERE u.userId=$userId";
            $r=$this->db->query($query);
            $r=$r->result();
            $r=(array)$r[0];
            $sesData=$r;
            $events=$this->eventCls->getAllEvents();
            $userEvents=$this->eventCls->getUserEvents($sesData['userId']);
            $uReg=$this->eventCls->getUserRegister($sesData['userId']);
            if(!empty($uReg)){
                $uReg=$uReg[0];
                }
            $data=array("userData"=>$sesData,"events"=>$events,"userEvents"=>$userEvents,"uReg"=>(array)$uReg);
            $this->load->view("profile_v",$data);
        }else{
            redirect(base_url());
        }
    }

    public function signIn(){
        $em=@$_POST['email'];
        $ps=@$_POST['password'];
        $ps=base64_encode($ps);
        $query ="SELECT u.fName,u.lName,u.userId,u.email,ui.userInterest,ui.userBio,ui.userCity,ui.userState,
        u.datastorePath, u.hasIcon FROM user AS u LEFT JOIN userinfo AS ui ON ui.userId=u.userId where email='$em' AND password='$ps'";
        $rs=$this->db->query($query);
        $result=$rs->result();
        if(!empty($result)){
            $result=(array)$result[0];
            $this->session->set_userdata(array("userData"=>$result));
        }
        $this->index();//now call the index function to determine where should the page should go
     }

     public function signOut(){
        $this->session->sess_destroy();
         redirect(base_url());
     }
     public function processEvents(){
        $this->load->model("event_MD","eventCls");
        $uid=@$_REQUEST['uid'];
        $eid=@$_REQUEST['eid'];
        $join=@$_REQUEST['join'];
        $wish=@$_REQUEST['wish'];
        $ppToken=@$_REQUEST['token'];
        $payerID=@$_REQUEST['PayerID'];
        var_dump($_REQUEST);
        //$volleyBall=@$_REQUEST['volleyball'];
        //$waterPong=@$_REQUEST['waterpong'];
        if(!empty($ppToken) && !empty($payerID)){
            $rs=$this->eventCls->getEventInfo($eid,"cost");
            $include="html/application/helpers/paypalfunctions.php";
            if(strpos($_SERVER['HTTP_HOST'],"localhost")===false){
                $include="application/helpers/paypalfunctions.php";
            }
            require_once $_SERVER['DOCUMENT_ROOT'].$include;
            $response=directPaymentConfirm(number_format($rs[0]->cost, 2, '.', ''),$ppToken,$payerID);
            $ack=$response['ACK'];
            if(strtoupper($ack)!="SUCCESS"){
                //direct user to an payment error page
                show_404();
                exit;
            }
        }
        $jrs=false;
        $rs=false;
        if($uid==null){
             $sesData=$this->session->userdata('userData');
            $uid=$sesData["userId"];
        }
            $params=array("uid"=>$uid,"eid"=>$eid);
            
            if($wish=="on"){
                $rs=$this->eventCls->saveWishEvent($params);
            }
             if($join=="on"){
                $jrs=$this->eventCls->saveJoinEvent($params);
             }
            if($rs===true ||$jrs===true){
                redirect(base_url()."/index.php/profile");
            }else{
                show_404();
            //redirect to an issue page
            }
     }
     public function updateUserProfile(){  
         $sesData=$this->session->userdata('userData');
         $userId=$sesData["userId"];
         $this->load->model("users_md","user");
         $result=$this->user->updateUserProfile($_POST,$userId);
         if($result===TRUE){
            redirect("/profile");
         }else{
            show_404();
         }
     }
     public function updateUserImage(){
        $this->load->model("users_md","users");
        $sesData=$this->session->userdata('userData');
        $path=$_SERVER['DOCUMENT_ROOT']."/application/datastore/".$this->users->getUserDatastorePath($sesData["userId"]);
        error_log($path);
	if($path){
            if(!file_exists($path)){
                $makeRs=mkdir($path,0777,true);
                if(!$makeRs){
                    echo "issue with make directory";
                    exit;
                }
            }
        }else{show_404();}
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'gif|jpg|png';
	$config['max_size']= '2048';
        $config['overwrite']=true;
	$config['max_width']  = '1024';
	$config['max_height']  = '768';
        $config['file_name']='userProfileImage.jpg';
        $this->load->library('upload',$config);
        $uploadRs=$this->upload->do_upload();
        if(!$uploadRs){
            show_404();
        }
        else{
            redirect("/profile");
        }

     }
}
?>
